package com.oro.converters;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.web.jsf.FacesContextUtils;

import com.oro.backingbeans.InserisciScuolaBean;
import com.oro.domain.BankBranch;

public class BankBranchStringConverter implements Converter {

	private List<BankBranch> branches;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) throws ConverterException {

		InserisciScuolaBean inserisciScuolaBean = (InserisciScuolaBean) FacesContextUtils
				.getWebApplicationContext(context).getBean(
						"inserisciScuolaBean");

		branches = inserisciScuolaBean.getSchoolInput().getBank().getBranches();

		for (BankBranch b : branches)
			if (b.toString().equals(value))
				return b;

		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {

		if (value != null)
			return ((BankBranch) value).toString();
		else
			return null;

	}

}
